package com.lhz.mail;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiHuaZhi
 * @Date: 2022/2/17 10:19
 * @Description:
 **/
@Service
public class MailService {
    @Resource
    private JavaMailSender jms;

    @Resource
    private TemplateEngine templateEngine;

    public String sendTestMail(String sender, String receiver, String subject, String text) {
        //建立邮件消息
        SimpleMailMessage mainMessage = new SimpleMailMessage();
        //发送方
        mainMessage.setFrom(sender);
        //接收方
        mainMessage.setTo(receiver);
        //发送的标题
        mainMessage.setSubject(subject);
        //发送的内容
        mainMessage.setText(text);
        //发送邮件
        jms.send(mainMessage);
        return "success";
    }

    public String sendHtml(String sender, String receiver, String subject, String content) {
        MimeMessage message = jms.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //发送方
            helper.setFrom(sender);
            //接收方
            helper.setTo(receiver);
            //邮件主题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(content, true);
            jms.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "sendHtml success";
    }

    public String sendAttachment(String sender, String receiver, String subject, String content, String filePath) {
        MimeMessage message = jms.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //发送方
            helper.setFrom(sender);
            //接收方
            helper.setTo(receiver);
            //邮件主题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(content, true);
            //邮件的附件
            FileSystemResource file = new FileSystemResource(new File(filePath));
            //获取附件的文件名
            String fileName = file.getFilename();
            helper.addAttachment(fileName, file);
            jms.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "sendAttachment success";
    }

    public String sendPicture(String sender, String receiver, String subject, String content,
                              String picPath, String picId) {
        MimeMessage message = jms.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //发送方
            helper.setFrom(sender);
            //接收方
            helper.setTo(receiver);
            //邮件主题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(content, true);
            //邮件的图片
            FileSystemResource file = new FileSystemResource(new File(picPath));
            helper.addInline(picId, file);
            jms.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "sendPicture success";
    }

    public String sendThymeleaf(String sender, String receiver, String subject) {
        try {
            //操作获取html模板
            Context context = new Context();
            // 设置html中的变量
            List<User> userList = new ArrayList<>();
            for (int i = 1; i <= 10; i++) {
                User user = new User();
                user.setUsername("admin-" + i);
                user.setNickname("用户-" + i);
                user.setAge(20 + i);
                user.setOdd(i % 2 == 0);
                userList.add(user);
            }
            // 设置html参数
            context.setVariable("userList", userList);
            //获取指定的html模板
            String result = templateEngine.process("mail", context);

            //创建一个发送复杂消息对象
            MimeMessage mimeMessage = jms.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);

            helper.setSubject(subject);
            //邮件内容,setText()第二个参数表示是否开启解析html
            helper.setText(result, true);
            helper.setFrom(sender);
            helper.setTo(receiver);

            jms.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return "sendThymeleaf success";
    }
}
